package utility;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;

import packing.State;

import com.mysql.jdbc.PreparedStatement;

public class ConnectMySQL {
	public Object javaObject;
	private Connection conn;
	private String user;
	private String password;

	public ConnectMySQL() {
		user = "root";
		password = "1234";
	}

	public void run() {
		try {
			Class.forName("com.mysql.jdbc.Driver");
			conn = DriverManager.getConnection(
					"jdbc:mysql://localhost/brutforce", user, password);

		} catch (SQLException | ClassNotFoundException ex) {
			Logger.getLogger(ConnectMySQL.class.getName()).log(Level.SEVERE,
					null, ex);
		}
	}

	public void saveObject(String table) throws Exception {
		try {
			PreparedStatement ps = null;
			String sql = null;

			ByteArrayOutputStream bos = new ByteArrayOutputStream();
			ObjectOutputStream oos = new ObjectOutputStream(bos);

			oos.writeObject(javaObject);
			oos.flush();
			oos.close();
			bos.close();

			byte[] data = bos.toByteArray();

			sql = "insert into " + table + "(javaObject) values(?)";
			ps = (PreparedStatement) conn.prepareStatement(sql);
			ps.setObject(1, data);
			ps.executeUpdate();

		} catch (Exception e) {
			e.printStackTrace();
		}

	}

	public Object getObject(String table, int i) throws Exception {
		Object rmObj = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = null;

		sql = "select * from " + table+" where id="+i+";";

		ps = (PreparedStatement) conn.prepareStatement(sql);

		rs = ps.executeQuery();

		if(rs.next()) {
			ByteArrayInputStream bais;

			ObjectInputStream ins;

			try {

				bais = new ByteArrayInputStream(rs.getBytes("javaObject"));

				ins = new ObjectInputStream(bais);

				rmObj = (Object) ins.readObject();

				ins.close();

			} catch (Exception e) {

				e.printStackTrace();
			}

		}

		return rmObj;
	}

	public List<Object> getObjectAll(String table) throws Exception {
		List<Object> listObj = new ArrayList<Object>();
		Object rmObj = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		String sql = null;

		sql = "select * from " + table;

		ps = (PreparedStatement) conn.prepareStatement(sql);

		rs = ps.executeQuery();

		while (rs.next()) {
			ByteArrayInputStream bais;

			ObjectInputStream ins;

			try {

				bais = new ByteArrayInputStream(rs.getBytes("javaObject"));

				ins = new ObjectInputStream(bais);

				rmObj = (Object) ins.readObject();
				listObj.add(rmObj);

				ins.close();

			} catch (Exception e) {

				e.printStackTrace();
			}

		}

		return listObj;
	}

	public Object getJavaObject() {
		return javaObject;
	}

	public void setJavaObject(Object javaObject) {
		this.javaObject = javaObject;
	}

	public int getCount(String table) throws Exception {
		Statement s = conn.createStatement();
		ResultSet r = s.executeQuery("SELECT COUNT(*) AS rowcount FROM "+table+";");
		r.next();
		int count = r.getInt("rowcount") ;
		r.close() ;
		System.out.println("MyTable has " + count + " row(s).");
		return count;

	}

}